//
//  QQLoginViewController.m
//  ScrollViews
//
//  Created by QQImac on 2018/4/2.
//  Copyright © 2018年 Mr.Lee. All rights reserved.
//

#import "QQLoginViewController.h"
#import "QQRegistersViewController.h"
#import "QQForgetPassWViewController.h"
#import "MainTabbarController.h"

@interface QQLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *numberTextF;
@property (weak, nonatomic) IBOutlet UITextField *passWordTextF;
@property (weak, nonatomic) IBOutlet UIButton *remindPSBtn;
@property (weak, nonatomic) IBOutlet UIButton *automaticLGBtn;
@property (nonatomic,assign) BOOL isSecure;

@end

@implementation QQLoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self loaddata];//判断登录状态
    // 添加监听
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:) name:@"UITextFieldTextDidChangeNotification" object:_numberTextF];
 
    _numberTextF.clearButtonMode = UITextFieldViewModeAlways;
    _passWordTextF.clearButtonMode = UITextFieldViewModeAlways;
    
    
//    LEEAlert.alert.config.LeeTitle(@"提示").LeeContent(@"请记住你自己的选择~~~~~~").LeeAction(@"正式", ^{
//        [[NSUserDefaults standardUserDefaults] setObject:@"http://fz.gate.szqqkj.cn" forKey:@"server"];
//        [[NSUserDefaults standardUserDefaults] setObject:@"http://fz.qjr.szqqkj.cn" forKey:@"webServer"];
//        [[NSUserDefaults standardUserDefaults] setObject:@"pro-file-store" forKey:@"OSS"];
//    }).LeeAction(@"测试", ^{
//        [[NSUserDefaults standardUserDefaults] setObject:@"http://test.gate.szqqkj.cn" forKey:@"server"];
////        [[NSUserDefaults standardUserDefaults] setObject:@"http://192.168.11.55:2222" forKey:@"server"];
//        [[NSUserDefaults standardUserDefaults] setObject:@"http://test.qjr.boss.szqqkj.cn" forKey:@"webServer"];
//        [[NSUserDefaults standardUserDefaults] setObject:@"id-photo" forKey:@"OSS"];
//    }).LeeShow();
}

-(void)loaddata{
    self.title = @"登录";
     [self.passWordTextF setSecureTextEntry:YES];
    
    if (kQQUserInfoManger.user_Number.length !=0){
        self.numberTextF.text = kQQUserInfoManger.user_Number;
    }
    if (kQQUserInfoManger.user_PassW.length !=0) {
        self.passWordTextF.text = kQQUserInfoManger.user_PassW;
        [self.passWordTextF setSecureTextEntry:YES];
        _remindPSBtn.selected = YES;
    }
    if(kQQUserInfoManger.isLogin){
        _automaticLGBtn.selected = YES;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (IBAction)rmPs:(UIButton *)sender {
    sender.selected = !sender.selected;
}
- (IBAction)zdLg:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
      _remindPSBtn.selected = YES;
    }
}

- (IBAction)forGetPassWorld:(id)sender {
    QQForgetPassWViewController* vc = [[QQForgetPassWViewController alloc]init];
    vc.type = 0;
    [self.navigationController pushViewController:vc animated:YES];

}
- (IBAction)reginsterClick:(id)sender {
    QQRegistersViewController* reVC = [[QQRegistersViewController alloc]init];
    [self.navigationController pushViewController:reVC animated:YES];
}
- (IBAction)loginClick:(id)sender {
    
    if ((_numberTextF.text.length==0)||(_passWordTextF.text.length==0)) {
        [[XNProgressHUD shared] showErrorWithTitle:@"请输入完整信息"];
        return;
    }
    if (!(_numberTextF.text.length == 11)) {
        [[XNProgressHUD shared] showErrorWithTitle:@"请注意手机号格式"];
        return;
    }
    if (_passWordTextF.text.length <= 0) {
        [[XNProgressHUD shared] showErrorWithTitle:@"请注意密码格式"];
        return;
    }
    if (_passWordTextF.text.length <= 3) {
        [[XNProgressHUD shared] showErrorWithTitle:@"请输入3-16位格式密码"];
        return;
    }
    NSDictionary *dic = @{ @"username":_numberTextF.text,
                          @"password":_passWordTextF.text,
                          };

    [[XNProgressHUD shared] showLoadingWithTitle:@" 正在登陆 " maskType:(XNProgressHUDMaskTypeBlack)];
    [[LMJRequestManager sharedManager] POST:KBaseURL(@"user/login") parameters:dic completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            NSDictionary* dict = response.responseModel.data;
            
            [QQUserInfo logout];
            //存储数据
            QQUserInfo *userInfo = [QQUserInfo mj_objectWithKeyValues:dict];
            userInfo.isPermissions = (userInfo.memberStatus == 1 && userInfo.status == 1) ? YES : NO;
            if (self.remindPSBtn.selected) {
                NSLog(@"记住密码");
                userInfo.user_Number = _numberTextF.text;
                userInfo.user_PassW = _passWordTextF.text;
                
            }else{
                userInfo.user_Number = nil;
                userInfo.user_PassW = nil;
            }
            if(self.automaticLGBtn.selected){
                userInfo.isLogin = YES;
            }else{
                userInfo.isLogin = NO;
            }
            
            [userInfo saveUserAccount];
            
            NSLog(@"%@",kQQUserInfoManger.realName);
            

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[XNProgressHUD shared] showSuccessWithTitle:@"登录成功"];
            });
            self.view.window.rootViewController = [[MainTabbarController alloc] init];
        } else {
            [[XNProgressHUD shared] showErrorWithTitle:response.errorMsg];
        }
        
        [[XNProgressHUD shared] dismiss];
    }];
    
}

- (IBAction)isShowThePassw:(UIButton*)btn {
     btn.selected = !btn.selected;
    BOOL isSecure = btn.selected;
    [self.passWordTextF setSecureTextEntry:isSecure];
    _isSecure = isSecure;
}
-(void)textFiledEditChanged:(NSNotification *)obj{
    UITextField *textField = (UITextField *)obj.object;
    
    NSString *toBeString = textField.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    
    if (toBeString.length ==0) {
        _passWordTextF.text = NULL;
    }
    
    
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > 11) {
                textField.text = [toBeString substringToIndex:11];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > 11) {
            textField.text = [toBeString substringToIndex:11];
        }
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:@"UITextFieldTextDidChangeNotification"
                                                 object:_numberTextF];
}

@end
